package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.CargoEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.UserOperating;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 杨畔
 * @date: 2020/12/29 18:14
 * @Description:
 */
public class CargoPublishHistoryViewModel extends AndroidViewModel {
    public MutableLiveData<ListResponse<List<CargoEntity>>> ldList = new MutableLiveData<>();

    public CargoPublishHistoryViewModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 获取货源详情，编辑时使用
     */
    public void getList(Context context, int page, @IntRange(from = 0, to = 1) int pattern) {
        Map<String, Object> map = new HashMap<>();
        if (UserOperating.getInstance().isCompany()) {
            map.put("companyNo", UserOperating.getInstance().getTeamOrCompanyNameNo());
            map.put("teamNo", "");
            map.put("terminal", "");
        } else if (UserOperating.getInstance().isTeam()) {
            map.put("companyNo", "");
            map.put("teamNo", UserOperating.getInstance().getTeamOrCompanyNameNo());
            map.put("terminal", "");
        } else if (UserOperating.getInstance().isPersonal()) {
            map.put("companyNo", "");
            map.put("teamNo", "");
            map.put("terminal", 3);
        }
        //货源发布模式 0 普通模式  1 竞价模式
        map.put("pattern", pattern);
        map.put("page", page);
        map.put("limit", 15);
        DialogHelper.showProgressDialog(context, "请求中...");
        NewAPIManager.getInstance().getLogisticsAPI().getHistoryCargo(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<CargoEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<CargoEntity>>> data) {
                ldList.postValue(data.getData());
            }

            @Override
            public void onFail() {
                ldList.postValue(null);
            }
        });
    }
}
